//加载事件
$(function(){
    //轮播图
    lunBoTu();
    newsHeader();
    updateNews();
    newsHot();
    newsList();
});
function lunBoTu(){
    //1.获得数据
    $.post('index',{'method':'imgList'},function(data){
        //2.渲染轮播图
        for (let i = 0; i < data.length ; i++) {
            //遍历每一个新闻对象
            let news = data[i];

            //渲染每一个图片
            let img = $("<IMG style=\"display: none;\" class=\"tu_img\" src=\""+news.imgUrl+"\" width=\"330\" height=\"208\" />");
            $("#lunBoTu .img").append(img);

            //渲染图片编号
            let number = i+1;
            let li;
            if(number==1){
                li =  $("<LI class=\"fouce\">"+number+"</LI>");
            } else {
                li =  $("<LI>"+number+"</LI>");
            }
            $("#lunBoTu .number").append(li);

            //渲染标题
            let title = $("<P style=\"height: 20px;\" class=\"tc\"><A href=\"#\" target=\"_blank\" title=\"\">"+news.title+"</A></P>");
            $("#lunBoTu .tit").append(title);
        }
    },"json")
}
function newsHeader() {
    $.getJSON('index', {"method":"getHeader"}, function (data) {
        $("#newsHeader h3 a").html(data.title)
        $("#newsHeader p").html(data.content + "<a href=\"http://www.baidu.com\">[查看全文]</a>")
    })
}
function updateNews(){
    $.getJSON('index',{"method":"updateNews"},function (data){
        for(let i = 0;i<data.length;i++){
            let tr ="<tr><td width=\"50%\"> <a href=\"#\" title=\"#\">"+data[i].title+"</a></td>";
            let t = ++i;
            if(t<data.length){
                tr+="<td width=\"50%\"> <a href=\"#\" title=\"#\">"+data[t].title+"</a></td>"
            }
            tr+="</tr>";
            $("#update-table").append($(tr));
        }
    })
}
function newsHot(){
    $.getJSON('index',{"method":"hotNews"},function (data){
        for(let i = 0;i<data.length;i++){
            let news = data[i];
            $("#hot-list").append($("<li><a href=\"#\" title=\"#\">"+news.title+"</a></li>>"));
        }

    })
}
function newsList(){
    $.getJSON('index',{"method":"newsList"},function(data){
        let result = '';
        for (let i = 0; i <data.length ; i++) {
            let type = data[i];
            let span4 = " <div class=\"span4\">";
            span4+="<div class=\"data_list news_list\">";
            span4+=" <div class=\"dataHeader\">"+type.typeName+"<span class=\"more\"><a href=\"#\">更多...</a></span></div>";
            span4+=" <div class=\"datas\">";
            span4+="<ul>";
            let newsList = type.news;

            if(newsList.length!=0){
                for (let j = 0; j <newsList.length; j++) {
                    let item = newsList[j];
                    span4+=" <li>&nbsp;<a href=\"#\" title=\"#\">"+item.title+"</a></li>";
                }
            }
            span4+="</ul>";
            span4+="</div>";
            span4+="</div>";
            span4+="</div>";
            result+=span4;
        }
        $("#news-list").html(result);
    })
}

